package com.parktech.sun.utils.action;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;


public class MailUtil {
	
	private String pName="";
	
	public MailUtil() {
	}

	public void send(SmtpConfig smtp, String toMmail, String title, String mailContent,String senderDepartName) throws UnsupportedEncodingException, MessagingException {
		Properties props = new Properties();
		Session sendMailSession;
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtp.getHost()); // smtp主机名。
		String u = smtp.getUser() + "@" + smtp.getDomain();
		props.put("mail.smtp.user", u); // 发送方邮件地址。
		props.put("mail.smtp.password", smtp.getPassword()); // 邮件密码。
		PopupAuthenticator popA = new PopupAuthenticator();// 邮件安全认证。
		popA.performCheck(smtp.getUser(), smtp.getPassword()); // 填写用户名及密码
		sendMailSession = Session.getInstance(props, popA);
		Message newMessage = new MimeMessage(sendMailSession);
		newMessage.setFrom(new InternetAddress(u,senderDepartName));
		newMessage.setRecipient(Message.RecipientType.TO,
				new InternetAddress(toMmail)); // 接收方邮件地址
		newMessage.setSubject(title);
		newMessage.setSentDate(new Date());
		newMessage.setContent(mailContent,"text/html; charset=UTF-8"); //发送正文
//		transport = sendMailSession.getTransport("smtp");
		Transport.send(newMessage);	
	}

    public void send(SmtpConfig smtp, String toMmail, String title, String mailContent, String senderName, String senderPassword,String senderDepartName) throws UnsupportedEncodingException, MessagingException {
		if(senderName==null || senderPassword ==null)
        {
            send(smtp,toMmail,title,mailContent,senderDepartName);
            return;
        }

        Properties props = new Properties();
		Session sendMailSession;
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtp.getHost()); // smtp主机名。
		String senderEmail = senderName.trim() + "@" + smtp.getDomain();
		props.put("mail.smtp.user", senderEmail); // 发送方邮件地址。
		props.put("mail.smtp.password", senderPassword); // 邮件密码。
		PopupAuthenticator popA = new PopupAuthenticator();// 邮件安全认证。
		popA.performCheck(senderName, senderPassword); // 填写用户名及密码
		sendMailSession = Session.getInstance(props, popA);
		Message newMessage = new MimeMessage(sendMailSession);
		newMessage.setFrom(new InternetAddress(senderEmail,senderDepartName));
		newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(toMmail)); // 接收方邮件地址
		newMessage.setSubject(title);
		newMessage.setSentDate(new Date());
		newMessage.setContent(mailContent,"text/html; charset=UTF-8"); //发送正文
		Transport.send(newMessage);
	}

	public class PopupAuthenticator extends Authenticator {
		String username = null;
		String password = null;

		public PopupAuthenticator() {
		}

		public PasswordAuthentication performCheck(String user, String pass) {
			username = user;
			password = pass;
			return getPasswordAuthentication();
		}

		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	}
}